package org.testile.placeholderscript;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import static org.testile.InstantiationUtils.getInstance;

/**
 *
 * @author hvizmar
 */
public class PlaceholderReplaceScriptEngineFactory implements ScriptEngineFactory {

	private Map<String, Object> parameters;

	private static List<PlaceholderTransformer> placeholderTransformers;

	static {
		placeholderTransformers = new ArrayList<PlaceholderTransformer>();
		Configuration configuration = getConfiguration("/placeholder-replace-script-engine.properties");
		String[] classes = configuration.getStringArray("placeholder.transformer.class");
		for (String classString : classes) {
			placeholderTransformers.add(getInstance(PlaceholderTransformer.class, classString));
		}
	}

	private static Configuration getConfiguration(String resourcePath) {
		try {
			URI uri = PlaceholderReplaceScriptEngineFactory.class.getResource(resourcePath).toURI();
			return new PropertiesConfiguration(new File(uri));
		} catch (ConfigurationException ex) {
			throw new IllegalStateException("Could not load congiguration from resource [" + resourcePath + "]", ex);
		} catch (URISyntaxException ex) {
			throw new IllegalArgumentException("Could not load congiguration from resource [" + resourcePath + "]", ex);
		}
	}

	public PlaceholderReplaceScriptEngineFactory() {
		parameters = new HashMap<String, Object>();
		parameters.put(ScriptEngine.ENGINE, "replace");
		parameters.put(ScriptEngine.ENGINE_VERSION, "0.1");
		parameters.put(ScriptEngine.NAME, "replace");
		parameters.put(ScriptEngine.LANGUAGE, "regexp pattern");
		parameters.put(ScriptEngine.LANGUAGE_VERSION, "1.0");
	}

	public String getEngineName() {
		return String.valueOf(parameters.get(ScriptEngine.ENGINE));
	}

	public String getEngineVersion() {
		return String.valueOf(parameters.get(ScriptEngine.ENGINE_VERSION));
	}

	public List<String> getExtensions() {
		return Collections.emptyList();
	}

	public List<String> getMimeTypes() {
		return Collections.emptyList();
	}

	public List<String> getNames() {
		return Collections.unmodifiableList(Arrays.asList("replace"));
	}

	public String getLanguageName() {
		return String.valueOf(parameters.get(ScriptEngine.LANGUAGE));
	}

	public String getLanguageVersion() {
		return String.valueOf(parameters.get(ScriptEngine.LANGUAGE_VERSION));
	}

	public Object getParameter(String key) {
		return parameters.get(key);
	}

	public String getMethodCallSyntax(String obj, String m, String... args) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public String getOutputStatement(String toDisplay) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public String getProgram(String... statements) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public ScriptEngine getScriptEngine() {
		return new PlaceholderReplaceScriptEngine(placeholderTransformers);
	}
}
